package com.wms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wms.bean.Coustomer;
import com.wms.bean.Orders;
import com.wms.config.Result;
import com.wms.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("order")
@CrossOrigin
public class OrderController {
    @Autowired
    OrderService orderService;
    @GetMapping("list")
    public Result getList() {
        List<Orders> list = orderService.list();
        return Result.success(list);
    }
    @PostMapping("add")
    public Result add(@RequestBody Orders orders) {
        orderService.save(orders);
        Orders ordersById = orderService.getById(orders.getOid());
        return Result.success("保存成功", ordersById);
    }
    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable("id") int id) {
        boolean b = orderService.removeById(id);
        if (b) {
            return Result.success("删除成功");
        } else {
            return Result.fail(201, "删除失败");
        }
    }
    @PutMapping("update")
    public Result update(@RequestBody Orders orders) {
        boolean b = orderService.updateById(orders);
        if (b) {
            Orders updatedOrder = orderService.getById(orders.getOid());
            return Result.success("修改成功", updatedOrder);
        } else {
            return Result.fail(201, "修改失败");
        }
    }
    @GetMapping("page")
    public Result getPage(Page page, @RequestParam("searchtext") String searchtext) {
        IPage<Orders> result;

        if (searchtext == null || searchtext.isEmpty()) {
            result =orderService.page(page);
        } else {
            QueryWrapper<Orders> wrapper = new QueryWrapper<Orders>();
            wrapper.like("oid", searchtext);

            result =orderService.page(page, wrapper); // ✅ 使用 wrapper 查询
        }

        return Result.success(result);
    }
}